# coding=utf-8

import smtplib  
from email.mime.text import MIMEText  
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart  


#邮件发送函数
#SMTPHost 邮件服务器
#fromAccount 发件邮箱
#fromPasswd 发件邮件授权码，注意并不是邮箱登录密码
#toAccount 收件邮箱
#subject 邮件标题
#content 邮件正文

def sendMail(SMTPHost, fromAccount, fromPasswd, toAccount, subject, content):    

    #构建邮件
    msg = MIMEMultipart()
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = fromAccount
    msg['To'] = toAccount

    content = '<html><body><h3>Hello,这是一封 测试 邮件。</h3>' + '<p><a href="gitee.com/simon4055">可以看看我的代码仓库</a></p>' + '</body></html>'
    puretext = MIMEText(content, 'html', 'utf-8')

    msg.attach(puretext)


    # 附件
    xlsxpart = MIMEApplication(open('tim.jpg', 'rb').read()) #这里填写你自己目录下的附件文件
    xlsxpart.add_header('Content-Disposition', 'attachment', filename='tim.jpg')
    msg.attach(xlsxpart)

    #使用smtplib模块发送邮件
    email_client = smtplib.SMTP(SMTPHost)
    email_client.login(fromAccount, fromPasswd)
    email_client.sendmail(fromAccount, toAccount, msg.as_string())
    email_client.quit()


#主函数
if __name__ == '__main__':
   sendMail(
       'smtp.qq.com',
       '********@qq.com',#你的邮箱
       '**********',#授权码
       'playactors@163.com',
       '很开心你来看我的文章了，欢迎关注哦。',
       '文章中有疑问的地方，欢迎留言与我联系哈，感谢你的支持！'
    )